package org.liveboardgames.agricola.model
{
    import com.adobe.cairngorm.model.ModelLocator;
    
    import mx.collections.ListCollectionView;
    
    import org.liveboardgames.agricola.control.delegates.action.AgricolaActionController;
    import org.liveboardgames.agricola.control.delegates.harvest.HarvestController;
    import org.liveboardgames.agricola.vo.Game;
    import org.liveboardgames.agricola.vo.action.Action;
    
    [Bindable]
    public class AgricolaModelLocator implements ModelLocator
    {        
        /** The unique instance of the model. */
        private static var modelLocator:AgricolaModelLocator;
        
        /** The representation of the game. It will be initialized later on. */
        public var game:Game;
        
        /** Everything that relates to popups. */
        public var popups:PopupModel = new PopupModel();
        
        /** The component in charge of handling all the action flows. */
        public var actionController:AgricolaActionController;
        
        public var harvestController:HarvestController;
        
        public function updateModel(changes:ListCollectionView):void
		{
			if (changes != null)
			{
				var modelUpdater:ModelUpdater = new ModelUpdater(this);
				
				for each (var change:Object in changes)
				{
					modelUpdater.updateModel(change);
				}
			}
		}
        
        public function reinitializePopupValues(action:Action):void
        {
            popups = new PopupModel();
            popups.currentAction = action;
        }
        
        public static function getInstance():AgricolaModelLocator
        {
            if (modelLocator == null)
            {
                modelLocator = new AgricolaModelLocator();
            }
            
            return modelLocator;
        }
        
        public function AgricolaModelLocator()
        {
            if (modelLocator != null)
            {
                throw new Error("Only one AgricolaModelLocator instance should be instantiated");
            }
        }
    
    }
}